//
//  MbRewardAd.m
//  MbAdsSdk
//
//  Created by 吴奶强 on 2021/12/17.
//

#import "MbInterstitialAd.h"
#import <BUAdSDK/BUAdSDK.h>
#import <KSAdSDK/KSAdSDK.h>
#import <ABUADSDK/ABUADSDK.h>
#import "GDTUnifiedInterstitialAd.h"


@interface MbInterstitialAd()<BUNativeExpressFullscreenVideoAdDelegate,ABUInterstitialProAdDelegate,GDTUnifiedInterstitialAdDelegate,KSInterstitialAdDelegate>

@property(nonatomic,strong)BUNativeExpressFullscreenVideoAd *ttFullVideoAd;
@property(nonatomic,strong)ABUInterstitialProAd *gromoreVideoAd;
@property(nonatomic,strong)GDTUnifiedInterstitialAd *gdtVideoAd;
@property(nonatomic,strong)KSInterstitialAd *ksVideoAd;

@property(nonatomic,assign)NSInteger index;

@property(nonatomic,strong)NSError *error;

@end

@implementation MbInterstitialAd


-(void)loadInterstitial{
    if(!self.adItems || self.adItems.count == self.index ){
        if(!self.error){
            self.error = [[NSError alloc]initWithDomain:@"配置信息错误" code:0 userInfo:nil];
        }
        if ([self.delegate respondsToSelector:@selector(InterstitialAdDidError:)]) {
            [self.delegate InterstitialAdDidError:self.error];
        }
        return;
    }
    AdItem *adItem = [self.adItems objectAtIndex:self.index];
    if(adItem.plat == Pangolin){
        self.ttFullVideoAd = [[BUNativeExpressFullscreenVideoAd alloc] initWithSlotID:adItem.codeNo];
        self.ttFullVideoAd.delegate = self;
        [self.ttFullVideoAd loadAdData];
        
    } else if(adItem.plat == GDT){
        
        self.gdtVideoAd = [[GDTUnifiedInterstitialAd alloc] initWithPlacementId:adItem.codeNo];
        self.gdtVideoAd.delegate = self;
        self.gdtVideoAd.videoMuted = NO; // 设置视频是否Mute
        [self.gdtVideoAd loadAd]; // 加载插屏半屏广告
        
    } else if(adItem.plat == KS){
        self.ksVideoAd = [[KSInterstitialAd alloc] initWithPosId:adItem.codeNo];
        self.ksVideoAd.delegate = self;
        [self.ksVideoAd loadAdData];
    } else if(adItem.plat == GroMore){
        self.gromoreVideoAd = [[ABUInterstitialProAd alloc] initWithAdUnitID:adItem.codeNo sizeForInterstitial:CGSizeMake(300, 300*3/2)];
        self.gromoreVideoAd.delegate = self;
        self.gromoreVideoAd.mutedIfCan = NO;//静音
        __weak typeof(self) weakself = self;
            //当前配置拉取成功，直接loadAdData
        if ([ABUAdSDKManager configDidLoad]) {
            [self.gromoreVideoAd loadAdData];
        } else {
            //当前配置未拉取成功，在成功之后会调用该callback
            [ABUAdSDKManager addConfigLoadSuccessObserver:self withAction:^(id  _Nonnull observer) {
                [weakself.gromoreVideoAd loadAdData];
            }];
        }
    }
    self.index ++;
}

- (void)showInterstitial:(UIViewController *)rootVC{
    
    if (self.gromoreVideoAd && self.gromoreVideoAd.isReady){
        [self.gromoreVideoAd showAdFromRootViewController:rootVC extroInfos:nil];
    }
    if (self.ttFullVideoAd){
        [self.ttFullVideoAd showAdFromRootViewController:rootVC];
    }
    if (self.gdtVideoAd && [self.gdtVideoAd isAdValid]){
        [self.gdtVideoAd presentAdFromRootViewController:rootVC];
    }

    if (self.ksVideoAd){
        [self.ksVideoAd showFromViewController:rootVC];
    }
    
}

#pragma mark 穿山甲回调
- (void)nativeExpressFullscreenVideoAdDidLoad:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    if ([self.delegate respondsToSelector:@selector(interstitialAdDidLoad)]) {
        [self.delegate interstitialAdDidLoad];
    }
}

- (void)nativeExpressFullscreenVideoAdDidDownLoadVideo:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    if ([self.delegate respondsToSelector:@selector(interstitialAdDidDownLoadVideo)]) {
        [self.delegate interstitialAdDidDownLoadVideo];
    }
}

- (void)nativeExpressFullscreenVideoAdDidVisible:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    if ([self.delegate respondsToSelector:@selector(interstitialAdDidVisible)]) {
        [self.delegate interstitialAdDidVisible];
    }
}

- (void)nativeExpressFullscreenVideoAd:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd didFailWithError:(NSError *)error{
    self.error = error;
    [self loadInterstitial];
}

#pragma mark 优量汇回调
- (void)unifiedInterstitialSuccessToLoadAd:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    if ([self.delegate respondsToSelector:@selector(interstitialAdDidLoad)]) {
        [self.delegate interstitialAdDidLoad];
    }
   
}

- (void)unifiedInterstitialRenderSuccess:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    if ([self.delegate respondsToSelector:@selector(interstitialAdDidDownLoadVideo)]) {
        [self.delegate interstitialAdDidDownLoadVideo];
    }
}

//- (void)unifiedInterstitialDidDownloadVideo:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
//    NSLog(@"1111111111111");
//    if ([self.delegate respondsToSelector:@selector(interstitialAdDidDownLoadVideo)]) {
//        [self.delegate interstitialAdDidDownLoadVideo];
//    }
//}
- (void)unifiedInterstitialDidPresentScreen:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    if ([self.delegate respondsToSelector:@selector(interstitialAdDidVisible)]) {
        [self.delegate interstitialAdDidVisible];
    }
}
- (void)unifiedInterstitialFailToLoadAd:(GDTUnifiedInterstitialAd *)unifiedInterstitial error:(NSError *)error{
    self.error = error;
    [self loadInterstitial];
}


#pragma mark 快手回调
- (void)ksad_interstitialAdDidLoad:(KSInterstitialAd *)interstitialAd{
    if ([self.delegate respondsToSelector:@selector(interstitialAdDidLoad)]) {
        [self.delegate interstitialAdDidLoad];
    }
}

- (void)ksad_interstitialAdDidVisible:(KSInterstitialAd *)interstitialAd{
    if ([self.delegate respondsToSelector:@selector(interstitialAdDidVisible)]) {
        [self.delegate interstitialAdDidVisible];
    }
}
- (void)ksad_interstitialAdRenderSuccess:(KSInterstitialAd *)interstitialAd{
    if ([self.delegate respondsToSelector:@selector(interstitialAdDidDownLoadVideo)]) {
        [self.delegate interstitialAdDidDownLoadVideo];
    }
}

- (void)ksad_interstitialAdRenderFail:(KSInterstitialAd *)interstitialAd error:(NSError *)error{
    self.error = error;
    [self loadInterstitial];
}

#pragma mark gromore回调
- (void)interstitialProAdDidLoad:(ABUInterstitialProAd *)interstitialProAd{
    if ([self.delegate respondsToSelector:@selector(interstitialAdDidLoad)]) {
        [self.delegate interstitialAdDidLoad];
    }
}
- (void)interstitialProAdDidDownLoadVideo:(ABUInterstitialProAd *)interstitialProAd{
    if ([self.delegate respondsToSelector:@selector(interstitialAdDidDownLoadVideo)]) {
        [self.delegate interstitialAdDidDownLoadVideo];
    }
}

- (void)interstitialProAdDidVisible:(ABUInterstitialProAd *)interstitialProAd{
    if ([self.delegate respondsToSelector:@selector(interstitialAdDidVisible)]) {
        [self.delegate interstitialAdDidVisible];
    }
}

- (void)interstitialProAd:(ABUInterstitialProAd *)interstitialProAd didFailWithError:(NSError *)error{
    self.error = error;
    [self loadInterstitial];
}


@end
